﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPF.CustomControl;

namespace WPF各种控件样式.TGrid
{
    /// <summary>
    /// Interaction logic for TGridBuilder.xaml
    /// </summary>
    public partial class TGridBuilder : Window
    {
        WPF.CustomControl.TGridBuilder tg = new WPF.CustomControl.TGridBuilder();
        public TGridBuilder()
        {
            InitializeComponent();
            Width = SystemParameters.WorkArea.Width * 3 / 4;
            Height = SystemParameters.WorkArea.Height * 3 / 4;
            Top = (SystemParameters.WorkArea.Height - Height) / 2;
            Left = (SystemParameters.WorkArea.Width - Width) / 2;

            grid_Content.Children.Add(tg);
            tg.DOMouseButtonDoubleClick += new MouseButtonHandler(tg_DOMouseButtonDoubleClick);
            SetTime(DateTime.Now);
            tb_formTime.Text = startTime.ToString("D");
            tb_endTime.Text = endTime.ToString("D");
            tg.StartTime = startTime;
        }
        public DateTime startTime;//上周日
        public DateTime endTime;//这周六
        /// <summary>
        /// 获取该周的周六和上周的周日
        /// </summary>
        /// <param name="time"></param>
        private void SetTime(DateTime time)
        {
            int week = (int)time.DayOfWeek;//第几周
            switch (week)
            {
                case 0:
                    startTime = time;
                    endTime = time.AddDays(6);
                    break;
                case 1:
                    startTime = time.AddDays(-1);
                    endTime = time.AddDays(5);
                    break;
                case 2:
                    startTime = time.AddDays(-2);
                    endTime = time.AddDays(4);
                    break;
                case 3:
                    startTime = time.AddDays(-3);
                    endTime = time.AddDays(3);
                    break;
                case 4:
                    startTime = time.AddDays(-4);
                    endTime = time.AddDays(2);
                    break;
                case 5:
                    startTime = time.AddDays(-5);
                    endTime = time.AddDays(1);
                    break;
                case 6:
                    startTime = time.AddDays(-6);
                    endTime = time;
                    break;
            }
        }

        void tg_DOMouseButtonDoubleClick(object sender, MouseButtonEventArgs e, TGridControl tControl)
        {
            if (tControl.ID == null)
            {
                tControl.ID = Guid.NewGuid().ToString();
            }
            tControl.TextContent = txt_Content.Text;
        }

        private void btn_add_Click(object sender, RoutedEventArgs e)
        {
            int rowIndex = Convert.ToInt32(txt_row.Text);
            int colIndex = Convert.ToInt32(txt_col.Text);
            TGridControl tControl = new TGridControl();
            tControl.ID = Guid.NewGuid().ToString();
            tControl.TextContent = txt_Content.Text;
            tg.SetBorderControl(rowIndex, colIndex, tControl);
        }

        private void btn_addTime_Click(object sender, RoutedEventArgs e)
        {
            SetTime(endTime.AddDays(7));
            tb_formTime.Text = startTime.ToString("D");
            tb_endTime.Text = endTime.ToString("D");
            tg.StartTime = startTime;
            tg.InitializeUI();
        }
    }

}
